import { request } from './request'
import ApiFn from './api-list.js'

const apiPrefix = ''
const Service = {}
const formatURL = function (URL, params) {
  var reg = /\/\{(.+?)\}/g
  if (reg.test(URL)) {
    return URL.replace(reg, ($0, $1) => {
      return '/' + params[$1]
    })
  }
  return URL
}

ApiFn(apiPrefix).forEach(v => {
  if (Service[v[0]]) {
    alert('接口名称:' + v[0] + '已被占用，请修改！', 'fail')
    return false
  }
  Service[v[0]] = params => {
    const url = formatURL(v[1], params)
    /* const settings = {
      url,
      method: v[2]
    }
    if (v[4]) {
      settings.headers['Content-Type'] = v[4]
    }
    if (v[2].toLowerCase() === 'get') {
      settings.params = params
    } else {
      settings.data = params || {}
    } */
    return request(url, v[2], params || {}, v[4])
  }
})
export default (window.Service = Service)
